<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 400px;
            border: 1px solid #000;
            border-collapse: collapse;
            margin: 0 auto;
        }

        td,
        th {
            text-align: center;
            border: 1px solid #000;
        }

        input {
            width: 50px;
        }

        .search {
            width: 600px;
            margin: 20px auto;
        }
    </style>
</head>

<body>
    <div class="search">
        按照价格查询:<input type="text" class='start'>-<input type="text" class='end'><button class="search-price">搜索</button>
        按照商品名称查询<input type="text" class="search-pro"><button>查询</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>产品名称</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <script>
        var data = [{

            id: 1,
            pname: '小米',
            price: 3999
        }, {
            id: 2,
            pname: 'oppo',
            price: 999
        }, {
            id: 3,
            pname: '荣耀',
            price: 1299
        }, {
            id: 4,
            pname: '华为',
            price: 1999
        }, ];
        var tbody = document.querySelector('tbody');
        var search_price = document.querySelector('.search-price');
        var start = document.querySelector('.start');
        var end = document.querySelector('.end');
        var product = document.querySelector('.product');
        var search_pro = document.querySelector('.search-pro');
        render(data);

        function render(data) {
            tbody.innerHTML = '';
            data.forEach(function (v) {
                var tr = document.createElement('tr');
                tr.innerHTML = `<td>${v.id}</td><td>${v.pname}</td><td>${v.price}</td>`
                tbody.appendChild(tr);
            })
        }
        search_price.addEventListener('click', function () {
            var res = data.filter(function (v) {
                return start.value < v.price && end.value >= v.price;
            });
            render(res);
        });
        search_pro.addEventListener('click', function () {
            var keyword = product.value.trim();
            if (keyword) {
                // 获取到第一个名称符合搜索条件的产品
                var res = [];
                data.some(function (v) {
                    if (v.pname.indexOf(product.value) != -1) {
                        // 找到了
                        res.push(v);
                        return true;
                    }
                });
            }
            render(res);
        })
    </script>
</body>

</html>